using System;
using System.Collections.Generic;
using System.Text;
using SP.Parameter;

namespace SP.HwLowProfileRS
{
    /// <summary>
    /// A simple base map feature
    /// </summary>
    public class ReflectionMapVertexFusion : AbstractFusion
    {
        public ReflectionMapVertexFusion()
        {           
            // param reflectionMap
            AddParameter(
                new TextureCubeParamter("reflectionMap", "reflectionMapSamp")
                .SetAddressModes(TextureParamter.AddressMode.Clamp, TextureParamter.AddressMode.Clamp, TextureParamter.AddressMode.Clamp)
                .SetSRGBTexture(true)
                .SetForceSamplerId(3)
                );

            AddParameter(
                new ScalarParameter("reflectance")
                .SetS(0.5f)
                .SetDesc("[0.0 - 2.0], the larger the value, the stronger the reflection effect")
                );
        }

        public override string Name
        {
            get { return "ReflectionMapVertex"; }
        }

        public override string Desc
        {
            get { return ""; }
        }

        public override Stage Stage
        {
            get { return Stage.Vertex0; }
        }

        public override int UVChannelCount
        {
            get { return 0; }
        }

        public override void Code(StringBuilder b, string platform)
        {
            // Hack!! Encode 1 - ndotv in Color.g
            AddLine(b, "pipeline.Color.g = 1 - pow(1 - dot(pipeline.N_Ws, pipeline.VDir_Ws), reflectance);");
            AddLine(b, "pipeline.ReflectionDir = reflect( -pipeline.VDir_Ws, pipeline.N_Ws );");
        }
    }

    public class ReflectionMapPixelFusion : AbstractFusion
    {
        public ReflectionMapPixelFusion()
        {           
        }

        public override string Name
        {
            get { return "ReflectionMapPixel"; }
        }

        public override string Desc
        {
            get { return ""; }
        }

        public override Stage Stage
        {
            get { return Stage.Override; }
        }

        public override int UVChannelCount
        {
            get { return 0; }
        }

        public override void Code(StringBuilder b, string platform)
        {
            AddLine(b, "pipeline.Reflection = texCUBE(reflectionMapSamp, input.ReflectionDir).rgb;");
            AddLine(b, "pipeline.FragColor = lerp(pipeline.FragColor, pipeline.Reflection, input.Color.g);");
        }
    }
}
